var app = getApp()
Page({
  data: {
    list:[],
    priceName:'零售价',
    addImg:'../../image/add.png',
    leftCurrentId:0,
    settlement:'卖货结算',
    goodsType:'',
    page:2,
    searchKey:'',
    accountId:''
  },
  onLoad: function (options) {
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId,
        accountId:res.data.id
      })
      this.getProductTypeList(res.data.agentId)
      this.getGoodsList('',this.data.searchKey)
      this.getSettlementCount()
    })
  },
  onShow(){
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId,
        accountId:res.data.id
      })
      this.getProductTypeList(res.data.agentId)
      this.getGoodsList('',this.data.searchKey)
      this.getSettlementCount()
    })
  },
  searchInput(e){
    let searchKey = e.detail.value
    this.setData({
      searchKey
    })
  },
  searchConfirm(){
    this.getGoodsList('',this.data.searchKey)
  },
  getGoodsList(pageNum,searchKey){
    wx.showLoading({
      title:'正在加载'
    })
    this.setData({
      list:[]
    })
    var that = this
    var data = {
      agentId:this.data.agentId,
      type:this.data.goodsType,
      pageNum,
      searchKey,
    }
    console.log(data)
    let list = []
    app.request('wx_app/mall/client/list',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        if (res.data.pageContent!='') {
          res.data.pageContent.forEach(item=>{
            list.push({
              name:item.name,
              price:item.price,
              url:item.url,
              id:item.sku_id,
              color:item.tag_color,
              companyName:item.product_tag,
              product_type:item.product_type,
              apply_to:item.apply_to,
              mt_id:item.mt_id,
              versionValues:item.versionValues,
              versionNameId:item.versionNameId
            })
            that.setData({
              list
            })
            wx.hideLoading()
          })
        }else{
          wx.hideLoading()
          app.errorMsg('没有搜到该物品')
        }
      }else{
        wx.hideLoading()
        app.errorMsg('没有搜到该物品')
      }
    })
  },
  //商品详情
  bindToDetail(e){
    wx.navigateTo({
      url:'goodsDetail?type=1&goodsType='+this.data.goodsType+
          '&id='+e.currentTarget.dataset.id+
          '&price='+e.currentTarget.dataset.price+
          '&product_type='+e.currentTarget.dataset.productType+
          '&applyTo='+e.currentTarget.dataset.applyTo+
          '&mtId='+e.currentTarget.dataset.mtId
    })
  },
  getProductTypeList(){
    let that = this
    let leftBtn = []
    let goodsType=''
    let data = {
      agentId:this.data.agentId,
      requestType:'client'
    }
    console.log(data)
    app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
      console.log(res)
      leftBtn.push({name:'全部',id:''})
      res.data.product_types.forEach(item=>{
        if (item.parent_id==0) {
          if(item.id!=1){
            leftBtn.push({name:item.name,id:item.id})
          }
        }
      })
      that.setData({
        leftBtn
      })
      goodsType = that.data.leftBtn[0].id
      that.setData({
        goodsType,
        page:2
      })
    })
  },
  bindLeftBtn(e){
    let type = e.currentTarget.dataset.type
    this.setData({
      leftCurrentId:e.currentTarget.dataset.id,
      goodsType:type,
      page:2
    })
    this.getGoodsList('1',this.data.searchKey)
  },
  loadMore(){
    wx.showLoading({
      title:'正在加载'
    })
    let page = this.data.page
    var that = this
    var data = {
      agentId:this.data.agentId,
      type:this.data.goodsType,
      pageNum:page,
      searchKey:this.data.searchKey
    }

    let list = this.data.list
    app.request('wx_app/mall/client/list',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {      
        res.data.pageContent.forEach(item=>{
          list.push({
            name:item.name,
            price:item.price,
            url:item.url,
            id:item.sku_id,
            companyName:item.product_tag,
            color:item.tag_color,
            apply_to:item.apply_to,
            mt_id:item.mt_id,
            versionValues:item.versionValues,
            versionNameId:item.versionNameId
          })
          that.setData({
            list
          })
        })
        wx.hideLoading()
      }else{
        wx.hideLoading()
        res.errorMsg(res.message)
      }
    })
    page++
    this.setData({
      page:page
    })
  },
  //获取购物车数量
  getSettlementCount(){
    let data = {
      accountId:this.data.accountId
    }
    var that = this
    app.request('wx_app/cart/product/list',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let count = 0
        res.data.carts.forEach(item=>{
          count+=item.count
        })
        this.setData({
          settlementCount:count
        })
      }
    })
  },
  //加入购物车
  bindPutInCart(e){
    const tap = e.currentTarget.dataset
    let data = {
      productType:tap.pt,
      accountId:this.data.accountId,
      skuId:tap.sku,
      productName:tap.pn,
      price:tap.price,
      count:1,
      versionValues:tap.vv,
      versionNameId:tap.vn,
      isOldForNew:0,
      applyTo:tap.ap,
      mtId:tap.mt
    }
    console.log(data)
    app.request('wx_app/cart/product/add',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        wx.showToast({
          title:'加入购物单成功！'
        })
        this.getSettlementCount()
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  bindToSettlement(){
    wx.navigateTo({
      url:'settlement'
    })
  }
})